﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace bookMaster.AssetSystem.Shell.Clases
{
    public class ControlFormat
    {
        public static void FormatControls(Control ctrl)
        {
            FormatControls(ctrl, null); 
        }

        public static void FormatControls(Control ctrl, string primaryKeys)
        {
            if (ctrl.GetType() == typeof(DataGridView))
            {
                FormatDataGridView((DataGridView)ctrl, primaryKeys); 
            }
            else if (ctrl is IContainerControl || ctrl is ScrollableControl || ctrl is Panel || ctrl is GroupBox)
            {
                foreach (Control c in ctrl.Controls)
                {
                    FormatControls(c, primaryKeys); 
                }
            } 
        }


        public static void FormatDataGridView(DataGridView grid, string primaryKeys)
        {
            if (grid.Tag != null && grid.Tag.ToString() != string.Empty)
            {
                return;
            }

            DataGridViewCellStyle dgvCellStyle = new DataGridViewCellStyle();
            grid.AllowUserToAddRows = false;
            grid.AllowUserToDeleteRows = false;
            grid.AllowUserToResizeRows = false;
            grid.AllowUserToResizeColumns = true;
            dgvCellStyle.BackColor = System.Drawing.Color.Gainsboro;
            grid.AlternatingRowsDefaultCellStyle = dgvCellStyle;
            grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;
            grid.BackgroundColor = System.Drawing.Color.White;
            grid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            grid.MultiSelect = false;
            grid.RowHeadersWidth = 25;
            grid.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            grid.RowTemplate.Height = 18;
            grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            foreach (DataGridViewColumn column in grid.Columns)
            {
                if (column.DataPropertyName != string.Empty)
                    column.ReadOnly = true;

                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

                if (column is DataGridViewComboBoxColumn)
                {
                    ((DataGridViewComboBoxColumn)column).DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                }

                if (!string.IsNullOrEmpty(primaryKeys))
                {
                    if (column.DataPropertyName != string.Empty && primaryKeys.Contains(column.DataPropertyName))
                    {
                        column.Visible = false;
                    } 
                }
            }
        }

        //public static void FormatTextBox(TextBox txt) { }

        //public static void FormatLabel(Label lbl) { }

        //public static void FormatComboBox(ComboBox cbo) { }

        //public static void FormatCheckBox(CheckBox chk) { }

        public static void RenombrarDataGridViewColumns(DataGridView grid, string nombreColumna, string headerText)
        {
            foreach (DataGridViewColumn column in grid.Columns)
            {
                if (column.DataPropertyName == nombreColumna)
                {
                    column.HeaderText = headerText;
                    break;
                }
            }
 
        }
    }
}
